import json
from enum import Enum
from json import JSONDecodeError
from typing import Optional, Dict

# Removed Dify specific Redis import
# from extensions.ext_redis import redis_client

# Simple in-memory cache for provider credentials
_provider_credentials_cache: Dict[str, dict] = {}

class ProviderCredentialsCacheType(Enum):
    PROVIDER = "provider"
    MODEL = "provider_model"
    LOAD_BALANCING_MODEL = "load_balancing_provider_model"

class ProviderCredentialsCache:
    def __init__(self, tenant_id: str, identity_id: str, cache_type: ProviderCredentialsCacheType):
        # For a standalone service, tenant_id and identity_id might be simplified or fixed
        # Using them to construct a unique key for the in-memory cache
        self.cache_key = f"{cache_type.value}_credentials:tenant_id:{tenant_id}:id:{identity_id}"

    def get(self) -> Optional[dict]:
        """
        Get cached model provider credentials from in-memory cache.

        :return:
        """
        return _provider_credentials_cache.get(self.cache_key)

    def set(self, credentials: dict) -> None:
        """
        Cache model provider credentials in-memory.

        :param credentials: provider credentials
        :return:
        """
        _provider_credentials_cache[self.cache_key] = credentials

    def delete(self) -> None:
        """
        Delete cached model provider credentials from in-memory cache.

        :return:
        """
        if self.cache_key in _provider_credentials_cache:
            del _provider_credentials_cache[self.cache_key]

